/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.taxigang.server;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

import com.google.appengine.api.datastore.Key;
import com.taxigang.shared.LoginInfo;
import com.taxigang.shared.TaxiGangInfo;
import com.taxigang.shared.WishRouteInfo;


@Entity
@Table(name = "WISHROUTE")
@NamedQueries({	@NamedQuery(	name = "WishRoute.findAll", 				query = "SELECT a FROM WishRoute a "), 
	@NamedQuery(	name = "WishRoute.findByStartProximity", 		query = "SELECT a FROM WishRoute a WHERE a.startGeobox = :geobox00 or a.startGeobox =:geobox01 or a.startGeobox =:geobox02 OR a.startGeobox = :geobox10 or a.startGeobox = :geobox11 or a.startGeobox = :geobox12 OR a.startGeobox = :geobox20 or a.startGeobox = :geobox21 or a.startGeobox = :geobox22"), 
	@NamedQuery(	name = "WishRoute.findByDestProximity", 		query = "SELECT a FROM WishRoute a WHERE a.destGeobox = :geobox00 or a.destGeobox =:geobox01 or a.destGeobox =:geobox02 OR a.destGeobox = :geobox10 or a.destGeobox = :geobox11 or a.destGeobox = :geobox12 OR a.destGeobox = :geobox20 or a.destGeobox = :geobox21 or a.destGeobox = :geobox22"), 
})
public class WishRoute implements Serializable {
	private static final long serialVersionUID = 1L;
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "ID", nullable = false)
	private Key key;

	@Column(name = "CREATIONTIME", nullable = false)
	private Date creationTime;
	@Column(name = "STARTGEOBOX", nullable = true)
	private long startGeobox;
	@Column(name = "DESTGEOBOX", nullable = true)
	private long destGeobox;
	@Column(name = "EMAIL", nullable = false)
	private String email;
	@Column(name = "LOCALE", nullable = false)
	private String locale;



	public WishRoute(TaxiGangInfo taxiGangInfo, LoginInfo loginInfo) {
		creationTime = new Date();
		setInfo(taxiGangInfo,loginInfo);
	}

	
	
	public void setInfo(TaxiGangInfo info, LoginInfo loginInfo) {
		this.email = loginInfo.getEmailAddress();
		this.locale = loginInfo.getLocale();

		startGeobox = GeoBox.getInstance().getIndex(info.getStartLat(), info.getStartLng());
		destGeobox = GeoBox.getInstance().getIndex(info.getDestinationLat(), info.getDestinationLng());

	}

	public WishRouteInfo getInfo() {
		WishRouteInfo info = new WishRouteInfo();
		String kind =key.getKind();
		String name =key.getName();
		info.setId(key.getId());
		info.setEmail(email);
		info.setLocale(locale);
		return info;
	}


	
	@Override
	public int hashCode() {
		int hash = 0;
		hash += (key != null ? key.hashCode() : 0);
		return hash;
	}

	@Override
	public boolean equals(Object object) {
		// TODO: Warning - this method won't work in the case the id fields are not set
		if (!(object instanceof WishRoute)) {
			return false;
		}
		WishRoute other = (WishRoute) object;
		if ((this.key == null && other.key != null) || (this.key != null && !this.key.equals(other.key))) {
			return false;
		}
		return true;
	}

	@Override
	public String toString() {
		return "taxigangsystem.beans.TaxiGang[taxigangid=" + key.getId() + "]";
	}
}
